﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.Runtime.InteropServices;
using FastReport;

namespace VechilePost
{
    public partial class MainForm : Form
    {
        string _IP;
        string lsh;
        string url;
        Report frt = new Report();

        public MainForm()
        {
            InitializeComponent();
            dgvInfo.AutoGenerateColumns = false;
            ReadConfig();
            frt.Load(Application.StartupPath + "\\新车报告.frx");
        }

        private void ReadConfig()
        {
            string filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "system.ini");//在当前程序路径创建
            SqlHelper.conStr = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3};",
                INIHelper.Read("通讯设置", "数据库地址", ".", filePath),
                INIHelper.Read("通讯设置", "数据库名", ".", filePath),
                INIHelper.Read("通讯设置", "数据库登录名", ".", filePath),
                INIHelper.Read("通讯设置", "数据库密码", ".", filePath));
            _IP = INIHelper.Read("通讯设置", "数据库地址", ".", filePath);
            url = INIHelper.Read("通讯设置", "上传地址", ".", filePath);
            string dly = INIHelper.Read("信息设置", "登陆员", "", filePath);
            string[] dlyList = dly.Split('|');
            cbcDLY.Items.AddRange(dlyList);
            string ycy = INIHelper.Read("信息设置", "引车员", "", filePath);
            string[] ycyList = ycy.Split('|');
            cbcYCY.Items.AddRange(ycyList);
            txtPZR.Text = INIHelper.Read("信息设置", "批准人", "", filePath);
            txtBZ.Text = INIHelper.Read("信息设置", "备注", "", filePath);
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {

            StringBuilder sql = new StringBuilder();
            sql.Append("SELECT AUTO.AT_HPHM,AUTO.AT_CLLX,Detects_SJA.DETECT_ID,Detects_SJA.DET_JYCS,Detects_SJA.DET_DATE,Detects_SJA.DET_Location ");
            sql.Append("From AUTO,Detects_SJA Where ");
            sql.Append("AUTO.AT_ID=Detects_SJA.AT_ID ");
            sql.AppendFormat("and Detects_SJA.DET_DATE between '{0}' and '{1}' ", dateTimePickerStart.Value.ToShortDateString() + " 00:00:00", dateTimePickerEnd.Value.ToShortDateString() + " 23:59:00");
            sql.Append("order by Detects_SJA.DETECT_ID desc ");
            DataTable dt = SqlHelper.GetTable(sql.ToString());
            
            if (dt.Rows.Count > 0)
            {
                //去除空格
                var dataRows = dt.AsEnumerable();
                foreach (var row in dataRows)
                {
                    var cellList = row.ItemArray.ToList();
                    row.ItemArray = cellList.Select(x => x.ToString().Trim()).ToArray();
                }
                dgvInfo.DataSource = dt;
                dgvInfo.ClearSelection();
            }
            else
            {
                dgvInfo.DataSource = null;
                MessageBox.Show("查询记录为空！");
            }

        }

        private void QueryInfo()
        {
            if (string.IsNullOrEmpty(lsh))
            {
                MessageBox.Show("检测流水号不能为空！");
                return;
            }
            try
            {
                Clear();
                StringBuilder sql = new StringBuilder();
                sql.Append("SELECT AUTO.*,Detects_SJA.*,Detects_SJB.*,Detects_SJC.* ");
                sql.Append("From AUTO,Detects_SJA,Detects_SJB,Detects_SJC Where ");
                sql.Append("AUTO.AT_ID=Detects_SJA.AT_ID ");
                sql.Append("and Detects_SJB.DETECT_ID=Detects_SJA.DETECT_ID ");
                sql.Append("and Detects_SJC.DETECT_ID=Detects_SJB.DETECT_ID ");
                sql.AppendFormat("and Detects_SJA.DETECT_ID='{0}'", lsh);

                DataTable dt = SqlHelper.GetTable(sql.ToString());

                if (dt.Rows.Count > 0)
                {
                    btnUpload.Enabled = true;
                    btnPrint.Enabled = true;
                    StringBuilder jcxm = new StringBuilder();

                    #region 车辆信息
                    txtVIN.Text = dt.Rows[0]["AT_HPHM"].ToString().Trim();
                    txtZCZ.Text = "2";
                    txtCLXH.Text = dt.Rows[0]["AT_CLXH"].ToString().Trim();
                    txtJCLB.Text = dt.Rows[0]["DET_JCLB"].ToString().Trim();
                    txtFDJH.Text = dt.Rows[0]["AT_FDJH"].ToString().Trim();
                    txtJCRQ.Text = dt.Rows[0]["DET_DATE"].ToString().Trim();
                    txtCLLX.Text = dt.Rows[0]["AT_CLLX"].ToString().Trim();


                    cbcQDXS.Text = dt.Rows[0]["AT_QDFS"].ToString().Trim();
                    cbcRLLX.Text = dt.Rows[0]["AT_RYZL"].ToString().Trim();
                    cbcXGXS.Text = dt.Rows[0]["AT_SUTYPE"].ToString().Trim() == "1" ? "独立悬架" : "非独立悬架";
                    cbcQZDZ.Text = dt.Rows[0]["AT_LAMP"].ToString().Trim();
                    cbcDLY.Text = dt.Rows[0]["DET_DLY"].ToString().Trim();
                    cbcYCY.Text = dt.Rows[0]["DET_YCY"].ToString().Trim();
                    cbcQZDNFTZ.Text = dt.Rows[0]["AT_QZD"].ToString().Trim() == "1" ? "能" : "否";

                    #endregion

                    #region ABS制动数据

                    txtTJQZ1.Text = GetState(dt.Rows[0]["DET_ABSTJQZ1"].ToString().Trim());
                    txtTJQY1.Text = GetState(dt.Rows[0]["DET_ABSTJQY1"].ToString().Trim());
                    txtTJQSFZF1.Text = GetState(dt.Rows[0]["DET_ABSTJQ1"].ToString().Trim());
                    txtTJQPD1.Text = GetState(dt.Rows[0]["DET_ABSTJQJL1"].ToString().Trim());

                    txtLSCGQZ1.Text = GetState(dt.Rows[0]["DET_ABSLSCGQZ1"].ToString().Trim());
                    txtLSCGQY1.Text = GetState(dt.Rows[0]["DET_ABSLSCGQY1"].ToString().Trim());
                    txtLSCGQSFZF1.Text = GetState(dt.Rows[0]["DET_ABSLSCGQ1"].ToString().Trim());
                    txtLSCGQPD1.Text = GetState(dt.Rows[0]["DET_ABSLSCGQJL1"].ToString().Trim());

                    txtQZCS.Text = dt.Rows[0]["DET_BRIKECS1"].ToString().Trim();
                    txtQZPD.Text = GetState(dt.Rows[0]["DET_ABSJL1"].ToString().Trim());


                    txtTJQZ2.Text = GetState(dt.Rows[0]["DET_ABSTJQZ2"].ToString().Trim());
                    txtTJQY2.Text = GetState(dt.Rows[0]["DET_ABSTJQY2"].ToString().Trim());
                    txtTJQSFZF2.Text = GetState(dt.Rows[0]["DET_ABSTJQ2"].ToString().Trim());
                    txtTJQPD2.Text = GetState(dt.Rows[0]["DET_ABSTJQJL2"].ToString().Trim());

                    txtLSCGQZ2.Text = GetState(dt.Rows[0]["DET_ABSLSCGQZ2"].ToString().Trim());
                    txtLSCGQY2.Text = GetState(dt.Rows[0]["DET_ABSLSCGQY2"].ToString().Trim());
                    txtLSCGQSFZF2.Text = GetState(dt.Rows[0]["DET_ABSLSCGQ2"].ToString().Trim());
                    txtLSCGQPD2.Text = GetState(dt.Rows[0]["DET_ABSLSCGQJL2"].ToString().Trim());

                    txtHZCS.Text = dt.Rows[0]["DET_BRIKECS2"].ToString().Trim();
                    txtHZPD.Text = GetState(dt.Rows[0]["DET_ABSJL2"].ToString().Trim());


                    if (!string.IsNullOrEmpty(txtTJQZ1.Text))
                    {
                        jcxm.Append("ABS,");
                    }

                    #endregion

                    #region 其他数据
                    txtCSB.Text = dt.Rows[0]["DET_SPEED"].ToString().Trim();
                    if (!string.IsNullOrEmpty(txtCSB.Text))
                    {
                        jcxm.Append("车速,");
                    }

                    txtSJ.Text = dt.Rows[0]["DET_LB"].ToString().Trim();
                    if (!string.IsNullOrEmpty(txtSJ.Text))
                    {
                        jcxm.Append("声级,");
                    }

                    txtCH.Text = dt.Rows[0]["DET_SLIP"].ToString().Trim();
                    if (!string.IsNullOrEmpty(txtCH.Text))
                    {
                        jcxm.Append("侧滑,");
                    }
                    jcxm.Append("外观,动态,底盘,");

                    txtJYJG.Text = "合格";

                    txtZCCS.Text = dt.Rows[0]["DET_JYCS"].ToString().Trim();

                    #endregion
                    #region 灯光数据 
                    txt_zd1.Text = dt.Rows[0]["DET_ZWDGQ"].ToString().Trim();
                    txt_zd2.Text = dt.Rows[0]["DET_ZWYGCZPYL"].ToString().Trim();
                    txt_zd3.Text = dt.Rows[0]["DET_ZWYGSPPY"].ToString().Trim();
                    txt_zd4.Text = dt.Rows[0]["DET_ZWJGCZPYL"].ToString().Trim();
                    txt_zd5.Text = dt.Rows[0]["DET_ZWJGSPPY"].ToString().Trim();
                    txt_zd6.Text = dt.Rows[0]["DET_ZWYGDG"].ToString().Trim();

                    txt_yd1.Text = dt.Rows[0]["DET_YWDGQ"].ToString().Trim();
                    txt_yd2.Text = dt.Rows[0]["DET_YWYGCZPYL"].ToString().Trim();
                    txt_yd3.Text = dt.Rows[0]["DET_YWYGSPPY"].ToString().Trim();
                    txt_yd4.Text = dt.Rows[0]["DET_YWJGCZPYL"].ToString().Trim();
                    txt_yd5.Text = dt.Rows[0]["DET_YWJGSPPY"].ToString().Trim();
                    txt_yd6.Text = dt.Rows[0]["DET_YWYGDG"].ToString().Trim();

                    jcxm.Append("灯光,");

                    #endregion
                    #region 制动数据

                    txtLHZ1.Text = dt.Rows[0]["DET_WEIGHTZ1"].ToString().Trim();
                    txtLHY1.Text = dt.Rows[0]["DET_WEIGHTY1"].ToString().Trim();
                    txtZZLZ1.Text = dt.Rows[0]["DET_ZZLVZ1"].ToString().Trim();
                    txtZZLY1.Text = dt.Rows[0]["DET_ZZLVY1"].ToString().Trim();
                    txtZDZZLZ1.Text = dt.Rows[0]["DET_BRAKEL1"].ToString().Trim();
                    txtZDZZLY1.Text = dt.Rows[0]["DET_BRAKEY1"].ToString().Trim();
                    txtGCCZ1.Text = dt.Rows[0]["DET_BRAKEDIFZ1"].ToString().Trim();
                    txtGCCY1.Text = dt.Rows[0]["DET_BRAKEDIFY1"].ToString().Trim();
                    txtBPHL1.Text = dt.Rows[0]["DET_BRIKEDIFF1"].ToString().Trim();
                    txtDXCS1.Text = dt.Rows[0]["DET_BRIKECS1"].ToString().Trim();
                    txtXMPD1.Text = "合格";

                    txtLHZ2.Text = dt.Rows[0]["DET_WEIGHTZ2"].ToString().Trim();
                    txtLHY2.Text = dt.Rows[0]["DET_WEIGHTY2"].ToString().Trim();
                    txtZZLZ2.Text = dt.Rows[0]["DET_ZZLVZ2"].ToString().Trim();
                    txtZZLY2.Text = dt.Rows[0]["DET_ZZLVY2"].ToString().Trim();
                    txtZDZZLZ2.Text = dt.Rows[0]["DET_BRAKEL2"].ToString().Trim();
                    txtZDZZLY2.Text = dt.Rows[0]["DET_BRAKEY2"].ToString().Trim();
                    txtGCCZ2.Text = dt.Rows[0]["DET_BRAKEDIFZ2"].ToString().Trim();
                    txtGCCY2.Text = dt.Rows[0]["DET_BRAKEDIFY2"].ToString().Trim();
                    txtBPHL2.Text = dt.Rows[0]["DET_BRIKEDIFF2"].ToString().Trim();
                    txtDXCS2.Text = dt.Rows[0]["DET_BRIKECS2"].ToString().Trim();

                    txtXMPD2.Text = "合格";
                    jcxm.Append("制动");

                    #endregion

                    txtTSJCXM.Text = jcxm.ToString();//检测项目
                }
                else
                {
                    MessageBox.Show("查询记录为空！");
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private string GetState(string zt)
        {
            switch (zt)
            {
                case "1": return "合格";
                case "2": return "不合格";
                default: return "";
            }
        }

        private void Clear()
        {
            foreach (Control item in groupBox1.Controls)
            {
                if (item is TextBox)
                {
                    TextBox txtCtl = item as TextBox;
                    txtCtl.Enabled = true;
                    txtCtl.Text = "";
                }
                if (item is ComboBox)
                {
                    ComboBox cbxCtl = item as ComboBox;
                    cbxCtl.Enabled = true;
                    cbxCtl.Text = "";
                }
            }

            foreach (Control item in groupBox3.Controls)
            {
                if (item is TextBox)
                {
                    TextBox txtCtl = item as TextBox;
                    txtCtl.Enabled = true;
                    txtCtl.Text = "";
                }
            }

            foreach (Control item in groupBox4.Controls)
            {
                if (item is TextBox)
                {
                    TextBox txtCtl = item as TextBox;
                    txtCtl.Enabled = true;
                    txtCtl.Text = "";
                }
            }

            foreach (Control item in groupBox5.Controls)
            {
                if (item is TextBox)
                {
                    TextBox txtCtl = item as TextBox;
                    txtCtl.Enabled = true;
                    txtCtl.Text = "";
                }
            }

            foreach (Control item in groupBox6.Controls)
            {
                if (item is TextBox)
                {
                    TextBox txtCtl = item as TextBox;
                    txtCtl.Enabled = true;
                    txtCtl.Text = "";
                }
            }

            foreach (Control item in groupBox7.Controls)
            {
                if (item is TextBox)
                {
                    TextBox txtCtl = item as TextBox;
                    txtCtl.Enabled = true;
                    txtCtl.Text = "";
                }
            }

            foreach (Control item in groupBox8.Controls)
            {
                if (item is TextBox)
                {
                    TextBox txtCtl = item as TextBox;
                    txtCtl.Enabled = true;
                    txtCtl.Text = "";
                }
            }

            foreach (Control item in groupBox10.Controls)
            {
                if (item is TextBox)
                {
                    TextBox txtCtl = item as TextBox;
                    txtCtl.Enabled = true;
                }
            }


            foreach (Control item in groupBox12.Controls)
            {
                if (item is TextBox)
                {
                    TextBox txtCtl = item as TextBox;
                    txtCtl.Enabled = true;
                    txtCtl.Text = "";
                }
            }

            foreach (Control item in groupBox9.Controls)
            {
                if (item is TextBox)
                {
                    TextBox txtCtl = item as TextBox;
                    txtCtl.Enabled = true;
                    txtCtl.Text = "";
                }
            }
            foreach (Control item in groupBox11.Controls)
            {
                if (item is TextBox)
                {
                    TextBox txtCtl = item as TextBox;
                    txtCtl.Enabled = true;
                    txtCtl.Text = "";
                }
            }
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            UploadData();
        }

        private void UploadData()
        {
            if (MessageBox.Show(string.Format("确定上传流水号：{0} 的车辆？", lsh), "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {

                    #region D000471VMJC1detail

                    D000471VMJC1detail d000471VMJC11 = new D000471VMJC1detail
                    {
                        F0000016 = "一轴制动",
                        F0000017 = txtLHZ1.Text,
                        F0000018 = txtLHY1.Text,
                        F0000019 = txtZDZZLZ1.Text,
                        F0000020 = txtZDZZLY1.Text,
                        F0000021 = txtGCCZ1.Text,
                        F0000022 = txtGCCY1.Text,
                        F0000023 = txtBPHL1.Text,
                        F0000024 = txtZZLZ1.Text,
                        F0000025 = txtZZLY1.Text,
                        F0000026 = txtXMPD1.Text,
                        F0000027 = txtDXCS1.Text
                    };
                    D000471VMJC1detail d000471VMJC12 = new D000471VMJC1detail
                    {
                        F0000016 = "二轴制动",
                        F0000017 = txtLHZ2.Text,
                        F0000018 = txtLHY2.Text,
                        F0000019 = txtZDZZLZ2.Text,
                        F0000020 = txtZDZZLY2.Text,
                        F0000021 = txtGCCZ2.Text,
                        F0000022 = txtGCCY2.Text,
                        F0000023 = txtBPHL2.Text,
                        F0000024 = txtZZLZ2.Text,
                        F0000025 = txtZZLY2.Text,
                        F0000026 = txtXMPD2.Text,
                        F0000027 = txtDXCS2.Text
                    };
                    D000471VMJC1detail d000471VMJC1ABS1 = new D000471VMJC1detail
                    {
                        F0000016 = "一轴ABS",
                        F0000017 = "一轴ABS,左调节器项目:" + txtTJQZ1.Text,
                        F0000018 = "一轴ABS,右调节器项目:" + txtTJQY1.Text,
                        F0000019 = "一轴ABS,左右调节器是否装反项目:" + txtTJQSFZF1.Text,
                        F0000020 = "一轴ABS,调节器项目:" + txtTJQPD1.Text,
                        F0000021 = "一轴ABS,左轮速传感器项目:" + txtLSCGQZ1.Text,
                        F0000022 = "一轴ABS,右轮速传感器项目:" + txtLSCGQY1.Text,
                        F0000023 = "一轴ABS,左右轮速传感器是否装反项目:" + txtLSCGQSFZF1.Text,
                        F0000024 = "一轴ABS,轮速传感器项目:" + txtLSCGQPD1.Text,
                        F0000025 = "一轴ABS,检测次数项目:" + txtQZCS.Text,
                        F0000026 = "一轴ABS,结果判定项目:" + txtQZPD.Text,
                        F0000027 = ""
                    };

                    D000471VMJC1detail d000471VMJC1ABS2 = new D000471VMJC1detail
                    {
                        F0000016 = "二轴ABS",
                        F0000017 = "二轴ABS,左调节器项目:" + txtTJQZ2.Text,
                        F0000018 = "二轴ABS,右调节器项目:" + txtTJQY2.Text,
                        F0000019 = "二轴ABS,左右调节器是否装反项目:" + txtTJQSFZF2.Text,
                        F0000020 = "二轴ABS,调节器项目:" + txtTJQPD2.Text,
                        F0000021 = "二轴ABS,左轮速传感器项目:" + txtLSCGQZ2.Text,
                        F0000022 = "二轴ABS,右轮速传感器项目:" + txtLSCGQY2.Text,
                        F0000023 = "二轴ABS,左右轮速传感器是否装反项目:" + txtLSCGQSFZF2.Text,
                        F0000024 = "二轴ABS,轮速传感器项目:" + txtLSCGQPD2.Text,
                        F0000025 = "二轴ABS,检测次数项目:" + txtHZCS.Text,
                        F0000026 = "二轴ABS,结果判定项目:" + txtHZPD.Text,
                        F0000027 = ""
                    };
                    List<D000471VMJC1detail> listC1 = new List<D000471VMJC1detail>();
                    listC1.Add(d000471VMJC11);
                    listC1.Add(d000471VMJC12);
                    listC1.Add(d000471VMJC1ABS1);
                    listC1.Add(d000471VMJC1ABS2);
                    #endregion

                    #region D000471VMJC2detail

                    D000471VMJC2detail d000471VMJC21 = new D000471VMJC2detail
                    {
                        F0000028 = "左灯光",
                        F0000029 = txt_zd2.Text,
                        F0000030 = txt_zd3.Text,
                        F0000031 = txt_zd4.Text,
                        F0000032 = txt_zd5.Text,
                        F0000033 = txt_zd6.Text,
                    };

                    D000471VMJC2detail d000471VMJC22 = new D000471VMJC2detail
                    {
                        F0000028 = "右灯光",
                        F0000029 = txt_yd2.Text,
                        F0000030 = txt_yd3.Text,
                        F0000031 = txt_yd4.Text,
                        F0000032 = txt_yd5.Text,
                        F0000033 = txt_yd6.Text,
                    };

                    List<D000471VMJC2detail> listC2 = new List<D000471VMJC2detail>();
                    listC2.Add(d000471VMJC21);
                    listC2.Add(d000471VMJC22);

                    #endregion

                    #region D000471VMJC3detail

                    D000471VMJC3detail d000471VMJC31 = new D000471VMJC3detail
                    {
                        F0000041 = "车辆外观检查",
                        F0000042 = "无",
                        F0000043 = "无",
                        F0000044 = cbcDLY.Text,
                    };
                    D000471VMJC3detail d000471VMJC32 = new D000471VMJC3detail
                    {
                        F0000041 = "底盘动态检验",
                        F0000042 = "无",
                        F0000043 = "无",
                        F0000044 = cbcDLY.Text,
                    };
                    D000471VMJC3detail d000471VMJC33 = new D000471VMJC3detail
                    {
                        F0000041 = "车辆底盘检查",
                        F0000042 = "无",
                        F0000043 = "无",
                        F0000044 = cbcDLY.Text,
                    };
                    List<D000471VMJC3detail> listC3 = new List<D000471VMJC3detail>();
                    listC3.Add(d000471VMJC31);
                    listC3.Add(d000471VMJC32);
                    listC3.Add(d000471VMJC33);
                    #endregion

                    #region VechileData

                    VechileData data = new VechileData
                    {
                        F0000001 = txtJCRQ.Text,
                        F0000002 = txtVIN.Text,//DateTime.Now.ToString("yyyyMMddHHmmss") + lsh,
                        F0000003 = txtCLLX.Text,
                        F0000004 = txtCLXH.Text,
                        F0000005 = txtFDJH.Text,
                        F0000006 = cbcRLLX.Text,
                        F0000007 = cbcQDXS.Text,
                        F0000008 = txtZCZ.Text,
                        F0000009 = cbcXGXS.Text,
                        F0000010 = cbcQZDZ.Text,
                        F0000011 = cbcQZDNFTZ.Text,
                        F0000012 = txtJCLB.Text,

                        F0000014 = cbcDLY.Text,
                        F0000015 = cbcYCY.Text,

                        F0000034 = txtCSB.Text,
                        F0000035 = txtCH.Text,
                        F0000036 = txtSJ.Text,
                        F0000037 = "",
                        F0000038 = "",
                        F0000039 = "",
                        F0000040 = "",
                        D000471VMJC1 = listC1,
                        D000471VMJC2 = listC2,
                        D000471VMJC3 = listC3,
                        F0000045 = txtJYJG.Text,
                        F0000046 = txtPZR.Text,
                        F0000047 = txtZCCS.Text,
                        F0000048 = txtBZ.Text,
                        F0000049 = txtVIN.Text,
                    };

                    List<VechileData> listVechileData = new List<VechileData>();
                    listVechileData.Add(data);
                    #endregion

                    SendModel sendModel = new SendModel();
                    sendModel.PushDataList = JsonConvert.SerializeObject(listVechileData);
                    string postData = JsonConvert.SerializeObject(sendModel);

                    string recMsg = HttpPostHelper.Post(url, postData);
                    if (recMsg.Contains("Successful"))
                    {
                        ReceiveModel receiveModel = JsonConvert.DeserializeObject<ReceiveModel>(recMsg);

                        if (receiveModel.ReturnData.ResponseDataList[0].Result == "OK")
                        {
                            StringBuilder sql = new StringBuilder();
                            sql.Append("update Detects_SJA set DET_Location='上传成功' ");
                            sql.AppendFormat("where DETECT_ID='{0}'", lsh);
                            SqlHelper.Update(sql.ToString());

                            MessageBox.Show("上传成功\r\n"+recMsg);
                        }
                        else
                        {
                            MessageBox.Show(receiveModel.ReturnData.ResponseDataList[0].ErrorMessage, "上传错误");
                        }

                    }
                    else
                    {
                        MessageBox.Show(recMsg);
                    }


                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void dgvInfo_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (dgvInfo.RowCount > 0)
                {
                    if (e.Button == MouseButtons.Right)
                    {
                        dgvInfo.ClearSelection();
                        dgvInfo.Rows[e.RowIndex].Selected = true;
                        dgvInfo.CurrentCell = dgvInfo.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);

                    }
                    lsh = dgvInfo.Rows[e.RowIndex].Cells[0].Value.ToString();
                    QueryInfo();
                }
            }
            catch { }
        }

        private void 打印报告单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvInfo.CurrentRow.Selected)
            {
                frt.RegisterData(GetData(lsh), "new");
                frt.GetDataSource("new").Enabled = true;
                frt.Prepare();
                frt.PrintSettings.ShowDialog = false;
                frt.Print();
            }
        }

        private void 预览报告单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvInfo.CurrentRow.Selected)
            {
                frt.RegisterData(GetData(lsh), "new");
                frt.GetDataSource("new").Enabled = true;
                frt.Show();
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (dgvInfo.CurrentRow.Selected)
            {
                UploadData();
                frt.RegisterData(GetData(lsh), "new");
                frt.GetDataSource("new").Enabled = true;
                frt.Prepare();
                frt.PrintSettings.ShowDialog = false;
                frt.Print();
            }
        }

        private DataTable GetData(string lsh)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("SELECT AUTO.*,Detects_SJA.*,Detects_SJB.* ");
            sql.Append(",Detects_SJC.DET_LB,Detects_SJC.DET_LAJL,Detects_SJC.DET_LBCS ");
            sql.Append("From AUTO,Detects_SJA,Detects_SJB,Detects_SJC Where ");
            sql.Append("AUTO.AT_ID=Detects_SJA.AT_ID ");
            sql.Append("and Detects_SJB.DETECT_ID=Detects_SJA.DETECT_ID ");
            sql.Append("and Detects_SJC.DETECT_ID=Detects_SJB.DETECT_ID ");
            sql.AppendFormat("and Detects_SJA.DETECT_ID='{0}'", lsh);
            return SqlHelper.GetTable(sql.ToString());

        }
    }
}
